package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhentao.api.schedule.ScheduleClientApi;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.schedule.dto.TaskDto;
import com.zhentao.model.wemwdia.pojo.WmNews;
import com.zhentao.service.WmNewsTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class WmNewsTaskServiceImpl implements WmNewsTaskService {
    @Autowired
    private ScheduleClientApi scheduleClientApi;
    @Autowired
    private WmNewsAutoScanServiceImpl wmNewsAutoScanService;
    @Override
    public void addNewsToTask(WmNews wmNews) {
        System.out.println("添加任务到延迟任务里面");
        TaskDto dto=new TaskDto();
        dto.setTaskType(1);
        dto.setPriority(1);
        dto.setExecuteTime(wmNews.getPublishTime().getTime());;
        dto.setParameters(JSON.toJSONBytes(wmNews));
        scheduleClientApi.addTask(dto);
    }
    //每秒钟执行一次
    @Scheduled(fixedRate = 1000)
    @Override
    public void scanNewsByTask() {
        System.out.println("执行提交审核的任务：" + System.currentTimeMillis());
        //从redis的list里面提取数据，添加的时候添加的是1，1
        ResponseResult result = scheduleClientApi.poll(1, 1);
        System.out.println("获取到redis里面的数据" + result);
        if(result.getCode() == 200 && result.getData() != null){
            String datajson = JSON.toJSONString(result.getData());
            TaskDto dto = JSON.parseObject(datajson, TaskDto.class);
            WmNews wmNews = JSON.parseObject(dto.getParameters(), WmNews.class);
            System.out.println("获取到对象"+wmNews);
            //执行自动审核
            wmNewsAutoScanService.autoScanWnMews(wmNews.getId());
        }
    }
}
